package cn.shadow.game.chess.CHnchess.chessman;


import cn.shadow.game.chess.CHnchess.board.Board;
import cn.shadow.game.chess.CHnchess.board.Line;
import cn.shadow.game.chess.CHnchess.board.Position;
import cn.shadow.game.chess.CHnchess.type.Camp;
import cn.shadow.game.chess.CHnchess.type.ChessType;

/**
 * 炮
 */
public class Pao extends AbstractChess implements Line {

    public Pao(Board board, Camp camp) {
        super(ChessType.PAO, board, camp);
    }


    /**
     * @param each  每一个点
     * @param enemy true 只添加敌方的点
     * @return
     */
    private boolean addPoint(Position each, boolean enemy) {
        Chess chess = each.getChess();
        if (chess != null) { //找到一个点
            if (chess.getCamp() == this.getCamp()) { //己方
                return true;
            } else { //敌方
                if (enemy) {
                    canSetPosition.add(each);
                    canAttackPosition.add(each);
                }
                return true;
            }
        }
        if (enemy) {
            canAttackPosition.add(each);
        } else {
            canSetPosition.add(each);
        }
        return false;
    }

    //向左
    public void addLeftPoint() {
        int row = this.getRow();
        int col = this.getCol();
        for (int j = col - 1; j >= 0; j--) {
            Position each = getBoard().getPoint(row, j);
            if (addPoint(each, false)) {
                for (int k = j - 1; k >= 0; k--) {
                    Position enemy = getBoard().getPoint(row, k);
                    if (addPoint(enemy, true)) {
                        break;
                    }

                }
                break;
            }
        }
    }

    public void addRightPoint() {
        int row = this.getRow();
        int col = this.getCol();
        for (int j = col + 1; j < Board.COL; j++) {
            Position each = getBoard().getPoint(row, j);
            if (addPoint(each, false)) {
                for (int k = j + 1; k < Board.COL; k++) {
                    Position enemy = getBoard().getPoint(row, k);
                    if (addPoint(enemy, true)) {
                        break;
                    }
                }
                break;
            }
        }
    }

    public void addUpPoint() {
        int row = this.getRow();
        int col = this.getCol();
        for (int i = row - 1; i >= 0; i--) {
            Position each = getBoard().getPoint(i, col);
            if (addPoint(each, false)) {
                for (int k = i - 1; k >= 0; k--) {
                    Position enemy = getBoard().getPoint(k, col);
                    if (addPoint(enemy, true)) {
                        break;
                    }
                }
                break;
            }
        }
    }

    public void addDownPoint() {
        int row = this.getRow();
        int col = this.getCol();
        for (int i = row + 1; i < Board.ROW; i++) {
            Position each = getBoard().getPoint(i, col); //直线上每一个点
            if (addPoint(each, false)) {
                for (int k = i + 1; k < Board.ROW; k++) {
                    Position enemy = getBoard().getPoint(k, col); //直线上每一个点
                    if (addPoint(enemy, true)) {
                        break;
                    }
                }
                break;
            }
        }
    }

}
